package com.yupi.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.yupi.project.common.BaseResponse;
import com.yupi.project.common.DeleteRequest;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.model.dto.visitor.VisitorAddRequest;
import com.yupi.project.model.dto.visitor.VisitorQueryRequest;
import com.yupi.project.model.dto.visitor.VisitorUpdateRequest;
import com.yupi.project.model.entity.Visitor;
import com.yupi.project.model.vo.VisitorVO;
import com.yupi.project.service.VisitorService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户资料接口
 *
 * @author yupi
 */
@RestController
@RequestMapping("/visitor")
public class VisitorController {
    
    @Resource
    private VisitorService visitorService;
    
    /**
     * 创建用户
     *
     * @param visitorAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addVisitor(@RequestBody VisitorAddRequest visitorAddRequest, HttpServletRequest request) {
        if (visitorAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long topUp = visitorAddRequest.getVisitorTopUp();
        long sale = visitorAddRequest.getVisitorSale();
        visitorAddRequest.setVisitorBalance(topUp - sale);
        Visitor visitor = new Visitor();
        BeanUtils.copyProperties(visitorAddRequest, visitor);
        boolean result = visitorService.save(visitor);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(visitor.getId());
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteVisitor(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = visitorService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     *
     * @param visitorUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateVisitor(@RequestBody VisitorUpdateRequest visitorUpdateRequest, HttpServletRequest request) {
        if (visitorUpdateRequest == null || visitorUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(visitorUpdateRequest.getVisitorTopUp()!=null && visitorUpdateRequest.getVisitorSale()!=null){
            long topUp = visitorUpdateRequest.getVisitorTopUp();
            long sale = visitorUpdateRequest.getVisitorSale();
            visitorUpdateRequest.setVisitorBalance(topUp - sale);
        }

        Visitor visitor = new Visitor();
        BeanUtils.copyProperties(visitorUpdateRequest, visitor);
        boolean result = visitorService.updateById(visitor);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取用户
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<VisitorVO> getVisitorById(int id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Visitor visitor = visitorService.getById(id);
        VisitorVO visitorVO = new VisitorVO();
        BeanUtils.copyProperties(visitor, visitorVO);
        return ResultUtils.success(visitorVO);
    }

    /**
     * 获取用户列表
     *
     * @param visitorQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<VisitorVO>> listVisitor(VisitorQueryRequest visitorQueryRequest, HttpServletRequest request) {
        Visitor visitorQuery = new Visitor();
        if (visitorQueryRequest != null) {
            BeanUtils.copyProperties(visitorQueryRequest, visitorQuery);
        }
        QueryWrapper<Visitor> queryWrapper = new QueryWrapper<>(visitorQuery);
        List<Visitor> visitorList = visitorService.list(queryWrapper);
        List<VisitorVO> visitorVOList = visitorList.stream().map(visitor -> {
            VisitorVO visitorVO = new VisitorVO();
            BeanUtils.copyProperties(visitor, visitorVO);
            return visitorVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(visitorVOList);
    }

    /**
     * 分页获取用户列表
     *
     * @param visitorQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<VisitorVO>> listVisitorByPage(VisitorQueryRequest visitorQueryRequest, HttpServletRequest request) {
        long current = 1;
        long size = 10;
        Visitor visitorQuery = new Visitor();
        if (visitorQueryRequest != null) {
            BeanUtils.copyProperties(visitorQueryRequest, visitorQuery);
            current = visitorQueryRequest.getCurrent();
            size = visitorQueryRequest.getPageSize();
        }
        QueryWrapper<Visitor> queryWrapper = new QueryWrapper<>(visitorQuery);
        Page<Visitor> visitorPage = visitorService.page(new Page<>(current, size), queryWrapper);
        Page<VisitorVO> visitorVOPage = new PageDTO<>(visitorPage.getCurrent(), visitorPage.getSize(), visitorPage.getTotal());
        List<VisitorVO> visitorVOList = visitorPage.getRecords().stream().map(visitor -> {
            VisitorVO visitorVO = new VisitorVO();
            BeanUtils.copyProperties(visitor, visitorVO);
            return visitorVO;
        }).collect(Collectors.toList());
        visitorVOPage.setRecords(visitorVOList);
        return ResultUtils.success(visitorVOPage);
    }
}
